package machineLearning;

import java.util.ArrayList;
import java.util.List;

import machineLearning.featurecalculator.DependencyFeatures;
import machineLearning.featurecalculator.NGrams;
import machineLearning.featurecalculator.NRCEmotionFeatures;

import rainbownlp.core.Artifact;
import rainbownlp.core.Phrase;
import rainbownlp.core.PhraseLink;
import rainbownlp.core.Setting;
import rainbownlp.machineLearning.IFeatureCalculator;
import rainbownlp.machineLearning.MLExample;
import rainbownlp.machineLearning.featurecalculator.sentence.SentenceSyntax;
import rainbownlp.util.FileUtil;
import rainbownlp.util.HibernateUtil;

public class SentenceExampleBuilder {
	public static String experimentGroup = "SemEval";
	public enum semEmotions{
		ANGER, DISGUST,
		FEAR, JOY,
		SADNESS, SURPRISE;
	}
	public enum semAllEmotions{
		ANGER, DISGUST,
		FEAR, JOY,
		SADNESS, SURPRISE,
		NOT_ANGER, NOT_DISGUST,
		NOT_FEAR, NOT_JOY,
		NOT_SADNESS, NOT_SURPRISE;
	}
	public static List<IFeatureCalculator> featureCalculators = 
		new ArrayList<IFeatureCalculator>();
	
	public static void main(String[] args) throws Exception
	{
//		SentenceExampleBuilder.createExamples(true, "SemEval");
		calculateAllFeatures(true, "SemEval");
		calculateAllFeatures(false, "SemEval");
	}
	

	private static void calculateAllFeatures(boolean forTrain, String experimental_group) throws Exception {
		
		featureCalculators.add(new SentenceSyntax());
		featureCalculators.add(new NGrams());
		featureCalculators.add(new DependencyFeatures());
		featureCalculators.add(new NRCEmotionFeatures());
		int counter = 0;
		List<MLExample> examples = MLExample.getAllExamples(forTrain);
		for(MLExample sent_example:examples)
		{
			sent_example.calculateFeatures(featureCalculators);
			counter++;
			FileUtil.logLine(null,"Features calculated for Sentence: "+counter+"/"+examples.size());
		}
		
	}

	
	
	
	public static void createExamples(boolean is_training_mode, String experimentGroup) throws Exception
	{
		List<Artifact> sentences = 
				Artifact.listByTypeAndForTrain(Artifact.Type.Sentence,is_training_mode);
		
		int counter = 0;
		HibernateUtil.startTransaction();
		for(Artifact sentence : sentences)
		{
//			MLExample.hibernateSession = HibernateUtil.clearSession(MLExample.hibernateSession);
			for (semEmotions e:semEmotions.values())
			{
				//create an example
				
				int expected_class = semAllEmotions.valueOf("NOT_"+e.toString()).ordinal();

				Setting.SaveInGetInstance = true;

				MLExample sent_example = 
						MLExample.getInstanceForSentence(sentence, experimentGroup,expected_class);
				sent_example.setExpectedClass(expected_class);
				sent_example.setRelatedArtifact(sentence);

				sent_example.setPredictedClass(-1);

				sent_example.setForTrain(is_training_mode);

				MLExample.saveExample(sent_example);

//				sent_example.calculateFeatures(featureCalculators);
				
			}

			FileUtil.logLine(null,"LinkExampleBuilder--------Sentence processed: "+counter);
			
			
		}
		HibernateUtil.endTransaction();
		HibernateUtil.clearLoaderSession();
		
	}

	
}
